<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class hutang extends My_Controller
{
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model('mdl_hutang', 'hutang');
		
	}
	function index()
	{
		if ($this->can_view() == FALSE){
			redirect('auth/failed');
		}
		
		$data['can_view'] 	= $this->can_view();
		$data['can_insert'] = $this->can_insert();
		$data['can_update'] = $this->can_update();
		$data['can_delete'] = $this->can_delete();
		
		$this->open();
		
		# config pagination
		$config['base_url'] = base_url().'index.php/hutang/index/';
		$config['total_rows'] = $this->db->count_all('daftar_hutang');
		$config['per_page'] = '20';
		$config['num_links'] = '5';
		$config['uri_segment'] = '3';
		
		$config['full_tag_open'] = '';
		$config['full_tag_close'] = '';
		
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		
		$config['cur_tag_open'] = '<li><a href="javascript:void(0)" class="current">';
		$config['cur_tag_close'] = '</a></li>';
		
		$config['prev_link'] = 'Prev';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		
		$config['next_link'] = 'Next';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		
		$config['last_link'] = 'Last';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		
		$config['first_link'] = 'First';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		
		$this->pagination->initialize($config);	
		# end config pagination
		
		# get data		
		$data['results'] = $this->hutang->getItem($config['per_page'], $this->uri->segment(3));
		//$data['sebagai'] = $sebagai;
		
		
		# load view
		$this->load->view('hutang/hutang_list.php', $data);
		
		$this->close();
	}		function pembayaran(){		if ($this->can_view() == FALSE){			redirect('auth/failed');		}				$data['can_view'] 	= $this->can_view();		$data['can_insert'] = $this->can_insert();		$data['can_update'] = $this->can_update();		$data['can_delete'] = $this->can_delete();				$this->open();				# config pagination		$config['base_url'] = base_url().'index.php/hutang/index/';		$config['total_rows'] = $this->db->count_all('daftar_hutang');		$config['per_page'] = '20';		$config['num_links'] = '5';		$config['uri_segment'] = '3';				$config['full_tag_open'] = '';		$config['full_tag_close'] = '';				$config['num_tag_open'] = '<li>';		$config['num_tag_close'] = '</li>';				$config['cur_tag_open'] = '<li><a href="javascript:void(0)" class="current">';		$config['cur_tag_close'] = '</a></li>';				$config['prev_link'] = 'Prev';		$config['prev_tag_open'] = '<li>';		$config['prev_tag_close'] = '</li>';				$config['next_link'] = 'Next';		$config['next_tag_open'] = '<li>';		$config['next_tag_close'] = '</li>';				$config['last_link'] = 'Last';		$config['last_tag_open'] = '<li>';		$config['last_tag_close'] = '</li>';				$config['first_link'] = 'First';		$config['first_tag_open'] = '<li>';		$config['first_tag_close'] = '</li>';				$this->pagination->initialize($config);			# end config pagination				# get data				$data['results'] = $this->hutang->getItem($config['per_page'], $this->uri->segment(3));		//$data['sebagai'] = $sebagai;						# load view		$this->load->view('hutang/pembayaran_hutang_list.php', $data);				$this->close();	}		function insert()	{		if ($this->can_insert() == FALSE){			redirect('auth/failed');		}						$this->open();		# Data		$data['id_barang'] = $this->input->post('id_barang');		$data['nama_barang'] = $this->input->post('nama_barang');		$data['id_jenis'] = $this->input->post('id_jenis');		$data['id_kategori'] = $this->input->post('id_kategori');		$data['id_satuan'] = $this->input->post('id_satuan');		$data['id_golongan'] = $this->input->post('id_golongan');		$data['hpp'] = $this->input->post('hpp');		$data['harga_toko'] = $this->input->post('harga_toko');		$data['harga_partai'] = $this->input->post('harga_partai');		$data['harga_cabang'] = $this->input->post('harga_cabang');				$data['is_hargatoko'] = $this->input->post('is_hargatoko');		$data['is_hargapartai'] = $this->input->post('is_hargapartai');		$data['is_hargajual'] = $this->input->post('is_hargajual');		$data['point_karyawan'] = $this->input->post('point_karyawan');		$data['point_member'] = $this->input->post('point_member');		$data['userid'] = get_userid();						# set rules validation		$this->form_validation->set_rules('nama_barang', 'nama_barang', 'callback_cek_nama');		$this->form_validation->set_rules('id_jenis', 'id_jenis', 'required');		$this->form_validation->set_rules('id_kategori', 'id_kategori', 'required');		$this->form_validation->set_rules('id_satuan', 'id_satuan', 'required');		$this->form_validation->set_rules('id_golongan', 'id_golongan', 'required');		$this->form_validation->set_rules('hpp', 'hpp', 'trim');		$this->form_validation->set_rules('harga_toko', 'harga_toko', 'trim|numeric');		$this->form_validation->set_rules('harga_partai', 'harga_partai', 'trim|numeric');		$this->form_validation->set_rules('harga_cabang', 'harga_cabang', 'trim|numeric');		$this->form_validation->set_rules('is_hargatoko', 'is_hargatoko', 'trim');		$this->form_validation->set_rules('is_hargapartai', 'is_hargapartai', 'trim');		$this->form_validation->set_rules('is_hargajual', 'is_hargajual', 'trim');		$this->form_validation->set_rules('point_karyawan', 'point_karyawan', 'trim|numeric');		$this->form_validation->set_rules('point_member', 'point_member', 'trim|numeric');						$this->form_validation->set_error_delimiters('<div class="error">', '</div>');				# set message validation		$this->form_validation->set_message('required', 'Field %s harus diisi!');		$this->form_validation->set_message('numeric', 'Field %s harus diisi hanya dengan angka!');						if ($this->form_validation->run() == FALSE){						$this->load->view('hutang/hutang_add',$data);					}else{				$this->hutang->insert($data);						$this->session->set_flashdata('message', 'Data Penerimaan Kas Berhasil disimpan.');			redirect('hutang');		}				$this->close();	}		function update($id)	{		$glid1=$this->uri->segment(3);		$glid2=$this->uri->segment(4);		$glid3=$this->uri->segment(5);		$id = $glid1 . '/' . $glid2 . '/' . $glid3;		$config['base_url'] = base_url().'index.php/hutang/index/';		$config['total_rows'] = $this->db->count_all('daftar_hutang');		$config['per_page'] = '20';		$config['num_links'] = '5';		$config['uri_segment'] = '3';				$config['full_tag_open'] = '';		$config['full_tag_close'] = '';				$config['num_tag_open'] = '<li>';		$config['num_tag_close'] = '</li>';				$config['cur_tag_open'] = '<li><a href="javascript:void(0)" class="current">';		$config['cur_tag_close'] = '</a></li>';				$config['prev_link'] = 'Prev';		$config['prev_tag_open'] = '<li>';		$config['prev_tag_close'] = '</li>';				$config['next_link'] = 'Next';		$config['next_tag_open'] = '<li>';		$config['next_tag_close'] = '</li>';				$config['last_link'] = 'Last';		$config['last_tag_open'] = '<li>';		$config['last_tag_close'] = '</li>';				$config['first_link'] = 'First';		$config['first_tag_open'] = '<li>';		$config['first_tag_close'] = '</li>';				$this->pagination->initialize($config);			# end config pagination				# get data				$data['results'] = $this->hutang->getItem_angsuran($id);		/*end list angsuran*/				if ($this->can_update() == FALSE){			redirect('auth/failed');		}				$this->open();				$data['result'] 		= $this->hutang->getItemById($id);		$data['id_pembelian']	= $id;		$this->load->view('hutang/pembayaran_hutang_edit', $data);		$this->close();	}
	function process_update()	{		if ($this->can_update() == FALSE){			redirect('auth/failed');		}		$this->open();		# Data		$sisa_hutang=0;		$data['id_pembelian'] = $this->input->post('id_pembelian');		$data['po_no'] = $this->input->post('po_no');				$data['id_supplier'] = $this->input->post('id_supplier');		$data['id_cabang'] = $this->input->post('id_cabang');		$data['total_tagihan'] = $this->input->post('total_tagihan');		$data['pembayaran'] = $this->input->post('pembayaran');		$data['cara_bayar'] = $this->input->post('cara_bayar');		$sisa_hutang = $data['total_tagihan'] - $data['pembayaran'];		$data['sisa'] = $this->input->post('sisa');				$data['tanggal'] = $this->input->post('tanggal');		$data['glid'] = $this->input->post('glid');				$data['id_coa'] = '5';		$data['userid'] = get_userid();		# set rules validation		$this->form_validation->set_rules('po_no', 'po_no', 'required');		$this->form_validation->set_rules('id_supplier', 'id_supplier', 'required');		$this->form_validation->set_rules('id_cabang', 'id_cabang', 'required');		$this->form_validation->set_rules('tanggal', 'tanggal', 'required');		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');				# set message validation		$this->form_validation->set_message('required', 'Field %s harus diisi!');		if ($this->form_validation->run() == FALSE){					$this->load->view('hutang/pembayaran_hutang_edit', $data); 				}else{			# update ke table angsuran_hutang			# -------------------------			$angsuran_hutang['KOUNIT'] 		= $data['id_cabang'];			//$angsuran_hutang['po_no'] 	= $data['po_no'];			$angsuran_hutang['TANGGAL'] 	= date('Y-m-d'); //$data['tanggal'];			$angsuran_hutang['GLID'] 		= $data['glid'];					$angsuran_hutang['KODE_PARTNER'] = $data['id_supplier'];						$angsuran_hutang['total'] 		= $data['total_tagihan'];			$angsuran_hutang['angsuran'] 	= $data['pembayaran'];			$angsuran_hutang['pembayaran'] 	= $data['cara_bayar'];			$angsuran_hutang['sisa'] 		= $sisa_hutang;			//$angsuran_hutang['userid'] 	= $data['userid'];			//$this->pembelian->update($angsuran_hutang['id_pembelian'], $angsuran_hutang);			$this->hutang->insert_angsuran($angsuran_hutang);									$this->db->flush_cache();						$update_hutang = array(								'JUMLAH' => $sisa_hutang							);				$this->db->where('GLID', $data['glid']);						$this->db->update('daftar_hutang', $update_hutang);						$this->db->flush_cache();						if ($sisa_hutang=='0'){				$update_pembelian = array(								'status_hutang' => '2'				);								}else{				$update_pembelian = array(								'status_hutang' => '1'				);					}			$this->db->where('po_no', $data['po_no']);						$this->db->update('pembelian', $update_pembelian);						$this->session->set_flashdata('message', 'Data Pembayaran Hutang Berhasil disimpan.');			redirect('hutang/pembayaran');		}				$this->close();			}
}